package com.ctrip.corp.obt.shard.sql.mybatis.interceptor.compatibility.constant;

import com.ctrip.corp.obt.generic.utils.StringPool;

import java.sql.JDBCType;
import java.util.HashSet;
import java.util.Set;

/**
 * 存放信创兼容的相关常量数值
 *
 * @author x.shen
 * @since 2024/5/6
 */
public class CompatibilityConstant {

    private CompatibilityConstant(){}

    /**
     * 需要解析兼容的列类型
     */
    public static final Set<String> TYPE_PATTERN = new HashSet<>();

    static {
        // kingbase开启了大小写敏感，所以需要加载大小写匹配
        TYPE_PATTERN.add(StringPool.SINGLE_QUOTE + JDBCType.BIT.getName() + StringPool.SINGLE_QUOTE);
        TYPE_PATTERN.add(StringPool.SINGLE_QUOTE + JDBCType.BIT.getName().toLowerCase() + StringPool.SINGLE_QUOTE);
        TYPE_PATTERN.add(StringPool.SINGLE_QUOTE + JDBCType.TIMESTAMP.getName() + StringPool.SINGLE_QUOTE);
        TYPE_PATTERN.add(StringPool.SINGLE_QUOTE + JDBCType.TIMESTAMP.getName().toLowerCase() + StringPool.SINGLE_QUOTE);
    }

    /**
     * kingbase SQL query 查询字段类型
     * SELECT table_schema, table_name, column_name FROM information_schema.columns WHERE data_type = 'bit' AND
     * table_schema = 'public';
     */
    public static final String KING_BASE_SQL =
            "SELECT table_schema, table_name, column_name, data_type FROM information_schema.columns WHERE data_type in (%s) AND table_schema = 'public';";
    /**
     * dm SQL query 查询字段类型
     * SELECT OWNER, TABLE_NAME, COLUMN_NAME, DATA_TYPE FROM ALL_TAB_COLUMNS WHERE DATA_TYPE = 'BIT' AND OWNER =
     * 'corpgovernment_standard';
     */
    public static final String DM_SQL =
            "SELECT OWNER, table_name, column_name, data_type FROM ALL_TAB_COLUMNS WHERE data_type in (%s) AND OWNER = '%s';";

    /**
     * 主键id
     */
    public static final String PRIMARY_KEY_ID = "id";
    public static final String VLAUE_NULL = "null";

    // ******************************************* 待兼容的函数 *******************************************//
    public static final String FUNCTION_NOW= "now";  // now(3)
    public static final String FUNCTION_CURRENT_TIMESTAMP= "current_timestamp"; // current_timestamp(3)
    public static final String FUNCTION_LAST_INSERT_ID= "last_insert_id"; // last_insert_id()

    // json 函数
    public static final String FUNCTION_JSON_LENGTH = "json_length"; // json_length(reconciliation_org_id_config) = 0
    public static final String FUNCTION_JSON_SEARCH = "json_search"; // json_search(reconciliation_org_id_config, 'ALL', '59b8bd70-f0d0-40f7-aba9-5d2afeebbe46') IS NOT NULL

    // GROUP_CONCAT 函数
    public static final String FUNCTION_GROUP_CONCAT = "group_concat";
    public static final String FUNCTION_GROUP_CONCAT_SEPARATOR = "separator";

    // ******************************************* Kingbase *******************************************//
    public static final String KB_FUNCTION_LAST_INSERT_ID= "LASTVAL";
    public static final String KB_FUNCTION_NOW= "now";
    public static final String KB_FUNCTION_CURRENT_TIMESTAMP= "current_timestamp";


    // ******************************************* dm *******************************************//
    public static final String DM_FUNCTION_JSON_LENGTH = "length"; // length(reconciliation_org_id_config) = 0
    public static final String DM_FUNCTION_JSON_SEARCH = "json_extract"; // json_extract(c1, '$.c')
    public static final String DM_FUNCTION_PREFIX_$ = "$."; // json_extract(c1, '$.c')
    public static final String DM_FUNCTION_GROUP_CONCAT = "wm_concat";
    public static final String DM_FUNCTION_GROUP_CONCAT_SEPARATOR = "replace";


    // ******************************************* insert *******************************************//
    public static final String PARAMETER_OBJECT_KEY_PREFIX = "param";


}
